﻿using System;
using System.Collections;
using System.IO;
using Assets.Scripts.Commom;
using Assets.Scripts.Net.Loader;
using Assets.Scripts.UI.Panel;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.UI.Item
{
    public class QueAudioItem:UIBase<QueAudioItem>
    {
        public Button PlayButton;
        public Button PauseButton;
        public AudioSource Audio;
        public AudioSeekBar AudioSeekBar;

        private int index;
        private string path;
        private string tempPath;//音频的临时路径

        protected override UIType UIType
        {
            get { return UIType.Item;}
        }

        protected override void InitOnAwake()
        {
            AutoFindComponent(transform,true);
            PlayButton.onClick.AddListener(OnPlayButtonClick);
            PauseButton.onClick.AddListener(OnPauseButtonClick);
            ExamPanel._Instance.AudioItems.Add(this);
        }

        /// <summary>
        /// 初始化并加载资源
        /// </summary>
        /// <param name="resPath"></param>
        public void Init(string resPath)
        {
            path = resPath;
            StartCoroutine(WWWLoadManager.Instance().LoadLocalResource(resPath, LoadAudio));   
        }

        private void LoadAudio(byte[]bytes)
        {
            ////1.写入一个临时的文件
            tempPath = getTempPath(path);
            File.WriteAllBytes(getTempPath(path), bytes);
            //2.读取路径中的文件
            StartCoroutine(LoadAuido(getTempPath(path), x =>
            {
                Audio.clip = x;
                AudioSeekBar.Init(Audio);
                File.Delete(tempPath);
            }));
        }

        private void IsPlay(bool isPlay)
        {
            AudioSeekBar.GetComponent<Slider>().interactable = isPlay;
            PauseButton.gameObject.SetActive(isPlay);
            PlayButton.gameObject.SetActive(!isPlay);
        }

        /// <summary>
        /// 加载音频资源
        /// </summary>
        /// <returns></returns>
        private IEnumerator LoadAuido(string resPath, Action<AudioClip> action)
        {
            string path = "file:///" + resPath;
            using (WWW www = new WWW(path))
            {
                yield return www;
                if (www.isDone && www.error == null)
                {
                    if (action != null) action(www.GetAudioClip(true));  //todo 有点问题         
                }
                else
                {
                    Debug.Log(www.error);
                }
            }
        }

        /// <summary>
        /// 得到临时的路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private string getTempPath(string path)
        {
            return path.Split('.')[0] + index +"."+path.Split('.')[1];
        }

        /// <summary>
        /// 播放按钮的点击事件
        /// </summary>
        private void OnPlayButtonClick()
        {
            ExamPanel._Instance.StopMediaPlay();
            IsPlay(true);
            Audio.Play();
        }

        /// <summary>
        /// 暂停按钮的点击事件
        /// </summary>
        private void OnPauseButtonClick()
        {
            IsPlay(false);
            Audio.Pause();
        }
    }
}